<?php
/**
*
* 版权所有：恰维网络<qwadmin.qiawei.com>
* 作    者：寒川<hanchuan@qiawei.com>
* 日    期：2016-09-20
* 版    本：1.0.0
* 功能说明：产品控制器。
*
**/

namespace Admin\Controller;
use Admin\Controller\ComController;
use Vendor\Tree;

class ArtificialController extends ComController {
public function add(){
		//获取所有的产品的分类信息
		$proartificial = M('proartificial')->field('id,pid,name')->order('o asc')->select();
		$tree = new Tree($proartificial);
		$str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
		$proartificial = $tree->get_tree(0,$str,0);
		$this->assign('proartificial',$proartificial);//导航
		$this -> display();
	}
		
	public function index($sid=0,$p=1){
		//分页获取产品的信息
		$sid = intval($sid);    //分类id
		$p = intval($p)>0?$p:1;//当前页码
		
		$artificial = M('artificial');
		$pagesize = 10;#每页数量
		$offset = $pagesize*($p-1);//计算记录偏移量
		$prefix = C('DB_PREFIX');
		if($sid){
			$where = "{$prefix}artificial.sid=$sid";
		}else{
			$where = '';
		}
		$count = $artificial->where($where)->count();
		$list  = $artificial->field("{$prefix}artificial.*,{$prefix}proartificial.name")->where($where)->order("{$prefix}artificial.pid desc")->join("{$prefix}proartificial ON {$prefix}proartificial.id = {$prefix}artificial.sid")->limit($offset.','.$pagesize)->select();
		
		$page	=	new \Think\Page($count,$pagesize); 
		$page = $page->show();
        $this->assign('prolist',$list);	
        $this->assign('page',$page);
		
		$this -> display();
	}
	
	public function del(){
		//删除时分两种情况，一种一次删除一行，一次删除多行
		//只要判断复选框  pids的值数字   pids数组删除多行
		$pids = isset($_REQUEST['pids'])?$_REQUEST['pids']:false;
		if($pids){
			if(is_array($pids)){
				$pids = implode(',',$pids);
				$map['pid']  = array('in',$pids);
			}else{
				$map = 'pid='.$pids;
			}
			if(M('artificial')->where($map)->delete()){
				addlog('删除产品，pID：'.$pids);
				$this->success('恭喜，产品删除成功！');
			}else{
				$this->error('参数错误！');
			}
		}else{
			$this->error('参数错误！');
		}
	}
	
	public function edit($pid){
		
		$pid = intval($pid);
		$artificial = M('artificial')->where('pid='.$pid)->find();
		if($artificial){
			
			$proartificial = M('proartificial')->field('id,pid,name')->order('o asc')->select();
			$tree = new Tree($proartificial);
			$str = "<option value=\$id \$selected>\$spacer\$name</option>"; //生成的形式
			$proartificial = $tree->get_tree(0,$str,$artificial['sid']);
			$this->assign('proartificial',$proartificial);//导航
			
			$this->assign('artificial',$artificial);
		}else{
			$this->error('参数错误！');
		}
		$this -> display();
	}
	
	public function update($pid=0){
		
		$pid = intval($pid);
		$data['sid'] = isset($_POST['sid'])?intval($_POST['sid']):0;//获取分类ID
		$data['pname'] = isset($_POST['pname'])?$_POST['pname']:false; //获取产品名称
		$data['keywords'] = I('post.keywords','','strip_tags');//获取产品关键字
		$data['description'] = I('post.description','','strip_tags');//产品描述
		$data['content'] = isset($_POST['content'])?$_POST['content']:false; 
		$data['thumbnail'] = I('post.thumbnail','','strip_tags');
		$data['t'] = time();
		if(!$data['sid'] or !$data['pname'] or !$data['content']){
			$this->error('警告！产品分类、产品名称及产品介绍为必填项目。');
		}
		if($pid){
			M('artificial')->data($data)->where('pid='.$pid)->save();
			addlog('编辑产品，PID：'.$pid);
			$this->success('恭喜！产品编辑成功！');
		}else{
			$pid = M('artificial')->data($data)->add();
			if($pid){
				addlog('新增产品，PID：'.$pid);
				$this->success('恭喜！产品新增成功！');
			}else{
				$this->error('抱歉，未知错误！');
			}
			
		}
	}
}